using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;

namespace MvcExtExample.ReportViews
{
    public partial class CQJMonthReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LogedUser"] == null)
                Response.Redirect("http://localhost:8132/home/index");
            if (!IsPostBack)
            {
                //获取所有工作区的名称
                this.WorkZoneDropDownList.DataSource = GetWorkZoneData();
                this.WorkZoneDropDownList.DataBind();
                //获取“第一作业区”下的生产大队列表
                this.SCDDDropDownList.DataSource = GetSCDDData("第一作业区");
                this.SCDDDropDownList.DataBind();
                //获取“第一生产大队”下的生产小队列表
                this.SCXDDropDownList.DataSource = GetSCDDData("第一采气大队");
                this.SCXDDropDownList.DataBind();
                //获取“第一生产大队”下的生产小队列表
                this.JHDropdownlist.DataSource = GetJHData("第一采气小队");
                this.JHDropdownlist.DataBind();
                this.productdate_box.Value = DateTime.Now.ToString("yyyy-MM");
                DataTable dt = GetData();
                ReportDataSource rds = new ReportDataSource("CQJMonthDataSet", dt);
                this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("..\\Reports\\CQJMonthReport.rdlc");
                ReportViewer1.KeepSessionAlive = true;//解决ASP.NET session has expired or could not be found问题
                ReportViewer1.LocalReport.DataSources.Clear();
                ReportViewer1.LocalReport.DataSources.Add(rds);
                ReportViewer1.LocalReport.Refresh();
            }
        }

        //从数据库读取报表所需数据
        private DataTable GetData()
        {
            List<string> Date = new List<string>(), Well_Section = new List<string>(), time = new List<string>(), DailyWater = new List<string>(), DailyOil = new List<string>(), DailyGas = new List<string>(), GasNipple = new List<string>(), Oil_Pressure = new List<string>(), Casing_Pressure = new List<string>(), Wellhead_Temperature = new List<string>(), Add_Water = new List<string>(), Comment = new List<string>();
            string productdate = this.productdate_box.Value;
            string workZone = this.WorkZoneDropDownList.SelectedValue;
            string SCDD = this.SCDDDropDownList.SelectedValue;
            string JH = this.JHDropdownlist.SelectedValue;
            string strSqlStr = "SELECT  * FROM [dbo].[Tb_Oil_Well]";
            //那么就把所有这个日期的抽油机井的数据读出来
            strSqlStr = "SELECT * FROM [Oil_Water_System].[dbo].[Tb_Gas_Well] WHERE SUBSTRING(CONVERT(VARCHAR(10),[Date]),0,8) ='" + productdate + "' AND [Well_ID]='" + JH + "'";
            framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
            DataSet DSet = cs.GetDataSet(strSqlStr);
            if (DSet != null)
            {
                DataTable DTable = DSet.Tables[0];
                for (int i = 0; i < DTable.Rows.Count; i++)
                {
                    DataRowView rowview = DSet.Tables[0].DefaultView[i];
                    Date.Add(Convert.ToDateTime(rowview["Date"].ToString()).ToShortDateString());
                    Well_Section.Add(rowview["Well_Section"].ToString());
                    time.Add(rowview["time"].ToString());
                    DailyWater.Add(rowview["DailyWater"].ToString());
                    DailyOil.Add(rowview["DailyOil"].ToString());
                    DailyGas.Add(rowview["DailyGas"].ToString());
                    GasNipple.Add(rowview["GasNipple"].ToString());
                    Oil_Pressure.Add(rowview["Oil_Pressure"].ToString());
                    Casing_Pressure.Add(rowview["Casing_Pressure"].ToString());
                    Wellhead_Temperature.Add(rowview["Wellhead_Temperature"].ToString());
                    Comment.Add(rowview["Comment"].ToString());
                }
            }
            DataTable dt = new DataTable("TB_CQJMonth");//这个名字是无关紧要的，xsd的DataTable也可以随意命名
            dt.Columns.Add(new DataColumn("Date", typeof(string)));
            dt.Columns.Add(new DataColumn("Well_Section", typeof(string)));
            dt.Columns.Add(new DataColumn("time", typeof(string)));
            dt.Columns.Add(new DataColumn("DailyWater", typeof(string)));
            dt.Columns.Add(new DataColumn("DailyOil", typeof(string)));
            dt.Columns.Add(new DataColumn("DailyGas", typeof(string)));
            dt.Columns.Add(new DataColumn("GasNipple", typeof(string)));
            dt.Columns.Add(new DataColumn("Oil_Pressure", typeof(string)));
            dt.Columns.Add(new DataColumn("Casing_Pressure", typeof(string)));
            dt.Columns.Add(new DataColumn("Wellhead_Temperature", typeof(string)));
            dt.Columns.Add(new DataColumn("Comment", typeof(string)));
            for (int i = 0; Date.Count > 0 && i < Date.Count; i++)
            {
                DataRow row = dt.NewRow();
                row["Date"] = Date[i];
                row["Well_Section"] = Well_Section[i];
                row["time"] = time[i];
                row["DailyWater"] = DailyWater[i];
                row["DailyOil"] = DailyOil[i];
                row["DailyGas"] = DailyGas[i];
                row["GasNipple"] = GasNipple[i];
                row["Oil_Pressure"] = Oil_Pressure[i];
                row["Casing_Pressure"] = Casing_Pressure[i];
                row["Wellhead_Temperature"] = Wellhead_Temperature[i];
                row["Comment"] = Comment[i];
                dt.Rows.Add(row);
            }
            return dt;
        }

        private List<string> GetJHData(string strSCXD)
        {
            List<string> CYJJList = new List<string>();
            string strSqlStr = "SELECT [Well_ID] FROM [dbo].[Tb_CommonWell]  WHERE [Dep_ID] IN (SELECT [Dep_ID] FROM [dbo].[Tb_Department] WHERE [Dep_Name] = '" + strSCXD + "')";
            framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
            DataSet DSet = cs.GetDataSet(strSqlStr);
            if (DSet != null)
            {
                DataTable DTable = DSet.Tables[0];
                for (int i = 0; i < DTable.Rows.Count; i++)
                {
                    DataRowView rowview = DSet.Tables[0].DefaultView[i];
                    CYJJList.Add(rowview["Well_ID"].ToString());
                }
            }
            return CYJJList;
        }

        //根据工作区名称得到生产大队列表
        private List<string> GetSCDDData(string strSCDD)
        {
            List<string> SCDDList = new List<string>();
            string strSqlStr = "SELECT [Dep_Name] FROM [dbo].[Tb_Department] WHERE [ParentDep_ID] IN (SELECT [Dep_ID] FROM [dbo].[Tb_Department] WHERE [Dep_Name] = '" + strSCDD + "') AND SUBSTRING([dbo].[Tb_Department].[Dep_ID],0,3)='CQ'";
            framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
            DataSet DSet = cs.GetDataSet(strSqlStr);
            if (DSet != null)
            {
                DataTable DTable = DSet.Tables[0];
                for (int i = 0; i < DTable.Rows.Count; i++)
                {
                    DataRowView rowview = DSet.Tables[0].DefaultView[i];
                    SCDDList.Add(rowview["Dep_Name"].ToString());
                }
            }
            return SCDDList;
        }

        //获取所有工作区的名称
        private List<string> GetWorkZoneData()
        {
            List<string> WorkZone = new List<string>();
            string strSqlStr = "SELECT [Dep_Name] FROM [dbo].[Tb_Department] WHERE [ParentDep_ID]='ALL'";
            framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
            DataSet DSet = cs.GetDataSet(strSqlStr);
            if (DSet != null)
            {
                DataTable DTable = DSet.Tables[0];
                for (int i = 0; i < DTable.Rows.Count; i++)
                {
                    DataRowView rowview = DSet.Tables[0].DefaultView[i];
                    WorkZone.Add(rowview["Dep_Name"].ToString());
                }
            }
            return WorkZone;
        }


        protected void WorkZoneDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SCDDDropDownList.DataSource = GetSCDDData(WorkZoneDropDownList.SelectedItem.Text);
            this.SCDDDropDownList.DataBind();
        }

        protected void SCDDDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SCXDDropDownList.DataSource = GetSCDDData(SCDDDropDownList.SelectedItem.Text);
            this.SCXDDropDownList.DataBind();
        }

        protected void SCXDDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            JHDropdownlist.DataSource = GetJHData(SCXDDropDownList.SelectedItem.Text);
            JHDropdownlist.DataBind();
        }
        protected void queryBtn_Click(object sender, EventArgs e)
        {
            DataTable dt = GetData();
            ReportDataSource rds = new ReportDataSource("CQJMonthDataSet", dt);
            this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("..\\Reports\\CQJMonthReport.rdlc");
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rds);
            ReportViewer1.LocalReport.Refresh();
        }
    }
}